package com.lanou.demo.backgrounddemo.service.impl;

import com.lanou.demo.backgrounddemo.mapper.ItemMapper;
import com.lanou.demo.backgrounddemo.service.ItemService;
import com.lanou.entity.entity.*;
import com.lanou.entity.utils.IdsResultUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 常宏盛
 * Date: 2019/7/11 11:01
 **/
@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ItemMapper itemMapper;

    @Override
    public int count() {
        return this.itemMapper.count();
    }

    @Override
    public List<ItemEntity> list(int page, int rows) {
        int offset = (page - 1) * rows;
        return this.itemMapper.list(offset, rows);
    }

    @Override
    public int itemSave(ItemEntity item) {
        return this.itemMapper.itemSave(item);
    }

    @Override
    public int itemDescSave(String desc, Long id) {
        return this.itemMapper.itemDescSave(desc, id);
    }

    @Override
    public int itemParamSave(Long id, String itemParams) {
        return this.itemMapper.itemParamSave(id, itemParams);
    }

    @Override
    public int delete(String[] ids) {
        List<Long> idsList = IdsResultUtils.idsResultList(ids);
        return this.itemMapper.delete(idsList);
    }

    @Override
    public int instock(String[] ids) {
        List<Long> idsList = IdsResultUtils.idsResultList(ids);
        List<ItemEntity> itemStatusList = this.itemMapper.itemStatusList(idsList);
        for (ItemEntity item : itemStatusList) {
            if (item.getStatus() == 2L) {
                return 0;
            }
        }
        this.itemMapper.instock(idsList);
        return 1;
    }

    @Override
    public int reshelf(String[] ids) {
        List<Long> idsList = IdsResultUtils.idsResultList(ids);
        List<ItemEntity> itemStatusList = this.itemMapper.itemStatusList(idsList);
        for (ItemEntity item : itemStatusList) {
            if (item.getStatus() == 1L) {
                return 0;
            }
        }
        this.itemMapper.reshelf(idsList);
        return 1;
    }

    @Override
    public ItemDescEntity itemDesc(Long id) {
        return this.itemMapper.itemDesc(id);
    }

    @Override
    public ItemParamItemEntity itemParam(Long id) {
        return this.itemMapper.itemParam(id);
    }

    @Override
    public List<TreeEntity> itemCatList(Long parentId) {
        List<ItemCatEntity> itemCatList = this.itemMapper.itemCatList(parentId);
        if (itemCatList.size() == 0) {
            return new ArrayList<>();
        }
        List<TreeEntity> treeList = new ArrayList<>();
        for (ItemCatEntity itemCat : itemCatList) {
            treeList.add(TreeEntity.builder()
                    .id(itemCat.getId())
                    .text(itemCat.getName())
                    .state(itemCat.getIsParent() == 1 ? "closed" : "open")
                    .build());
        }
        return treeList;
    }

    @Override
    public int update(ItemEntity item) {
        return this.itemMapper.update(item);
    }

    @Override
    public int updateItemParamData(String itemParams, Long itemParamId) {
        return this.itemMapper.updateItemParamData(itemParams, itemParamId);
    }

}
